package com.carleasoft.mps.forum.business.answer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.carleasoft.mps.core.user.ThreadUserContext;
import com.carleasoft.mps.core.user.UserCacheData;
import com.carleasoft.mps.forum.business.answer.bean.ForumPlate;
import com.carleasoft.mps.forum.business.answer.bean.po.ForumPlatePo;
import com.carleasoft.mps.forum.business.answer.bean.po.ForumUserPo;
import com.carleasoft.mps.forum.business.answer.mapper.ForumPlateMapper;
import com.carleasoft.mps.forum.business.answer.service.ForumPlateService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zang
 * @since 2023-05-09
 */
@Service
public class ForumPlateServiceImpl extends ServiceImpl<ForumPlateMapper, ForumPlate> implements ForumPlateService {

    @Override
    public void saveForumPlate(ForumPlate forumPlate) {
        UserCacheData user = ThreadUserContext.getCurrentCacheUser();
        forumPlate.setIsDelete(0);
        forumPlate.setIsApprove(1);
        forumPlate.setIsUsed(1);
        forumPlate.setPlateCode(UUID.randomUUID().toString());
        forumPlate.setCreatePersonCode(user.getUserCode());
        forumPlate.setCreatePersonName(user.getUserName());
        forumPlate.setCreateTime(new Date());
        this.save(forumPlate);
    }


    @Override
    public void updateForumPlate(ForumPlate forumPlate) {
        UserCacheData user = ThreadUserContext.getCurrentCacheUser();
        forumPlate.setUpdatePersonCode(user.getUserCode());
        forumPlate.setUpdatePersonName(user.getUserName());
        forumPlate.setUpdateTime(new Date());
        this.updateById(forumPlate);
    }

    @Override
    public Page<ForumPlate> selForumPlate(Page page, ForumPlatePo forumPlatePo) {
        long id = Thread.currentThread().getId();
        Page<ForumPlate> res =  this.getBaseMapper().selForumPlate(page,forumPlatePo);
        return res;
    }

    @Override
    public ForumPlate detailForumPlate(ForumPlatePo forumPlatePo) {
        ForumPlate plate =  this.getBaseMapper().detailForumPlate(forumPlatePo.getPlateCode());
        return plate;
    }

    @Override
    public void updateForumPlateApprove(ForumPlatePo forumPlatePo) {
        UserCacheData user = ThreadUserContext.getCurrentCacheUser();
        this.update(new UpdateWrapper<ForumPlate>()
                .set("UPDATE_PERSON_CODE",user.getUserCode())
                .set("UPDATE_PERSON_NAME",user.getUserName())
                .set("UPDATE_TIME",new Date())
                .set("IS_APPROVE",forumPlatePo.getIsApprove())
                .eq("PLATE_CODE",forumPlatePo.getPlateCode())
        );
    }

    @Override
    public void updateForumPlateStatus(ForumPlatePo forumPlatePo) {
        UserCacheData user = ThreadUserContext.getCurrentCacheUser();
        this.update(new UpdateWrapper<ForumPlate>()
                .set("UPDATE_PERSON_CODE",user.getUserCode())
                .set("UPDATE_PERSON_NAME",user.getUserName())
                .set("UPDATE_TIME",new Date())
                .set("IS_USED",forumPlatePo.getIsUsed())
                .eq("PLATE_CODE",forumPlatePo.getPlateCode())
        );

    }

    @Override
    public void deleteForumPlate(ForumPlatePo forumPlatePo) {
        UserCacheData user = ThreadUserContext.getCurrentCacheUser();
        this.update(new UpdateWrapper<ForumPlate>()
                .set("UPDATE_PERSON_CODE",user.getUserCode())
                .set("UPDATE_PERSON_NAME",user.getUserName())
                .set("UPDATE_TIME",new Date())
                .set("IS_DELETE",1)
                .in("PLATE_CODE",forumPlatePo.getPlateCodes())
        );
    }

    @Override
    public Page<ForumPlate> pageMinePlate(Page page, ForumUserPo forumUserPo) {
        Page<ForumPlate> res = this.getBaseMapper().pageMinePlate(page,forumUserPo);
        return res;
    }
}
